package com.itboye.sunsun.shop.ui;

import java.util.ArrayList;
import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.FenGuanGoodsAdapter;
import com.itboye.sunsun.beans.FenGuanGoodsBean;
import com.itboye.sunsun.beans.FenGuanGoodsBean.FenGuanItemBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.shop.ui.ShopActivity.MyXlistenr;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class ShopSouSuoActivity extends BaseActivity {

	EditText sousuo;
	private View paixu;
	private View shaixuan;
	private TextView paixuTitle;
	private View shaixuanInput;
	private View sortSelect;
	 
	private String parentId;
 
	private List<FenGuanItemBean> dataList = new ArrayList<FenGuanItemBean>();

	private FenGuanGoodsAdapter adapter;

	private String minCost;
	private String maxCost;

	XListView xlistview;
	private int pageNum = 1;

	/**
	 * 综合排序
	 */
	private static final String SORT_COMPREHENSIVE = "10";

	/**
	 * 销量排序
	 */
	private static final String SORT_SALE = "11";

	/**
	 * 价格从低到高
	 */
	private static final String SORT_PRICE_ASC = "12";

	/**
	 * 价格从高到低
	 */
	private static final String SORT_PRICE_DESC = "13";

	private String order = SORT_COMPREHENSIVE;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_sousuo);
		adapter = new FenGuanGoodsAdapter(dataList, this);
		xlistview.setAdapter(adapter);

		init();
		

	}

	private void init() {
		// TODO Auto-generated method stub
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		sortSelect.findViewById(R.id.first).setOnClickListener(sortListenr);
		sortSelect.findViewById(R.id.second).setOnClickListener(sortListenr);
		sortSelect.findViewById(R.id.three).setOnClickListener(sortListenr);
		sortSelect.findViewById(R.id.four).setOnClickListener(sortListenr);

		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pullDown();
			}

			@Override
			public void onLoadMore() {
				pullUp();
			}

		});
		
		paixu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sortSelect.setVisibility(View.VISIBLE);
				shaixuanInput.setVisibility(View.GONE);
				
				showAndHideCheck(order);
			}
		});
		
		shaixuan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sortSelect.setVisibility(View.GONE);
				shaixuanInput.setVisibility(View.VISIBLE);
				
				((EditText)shaixuanInput.findViewById(R.id.minPrice)).setText(minCost);
				
				((EditText)shaixuanInput.findViewById(R.id.maxPrice)).setText(maxCost);
				
			}
		});
		
		shaixuanInput.findViewById(R.id.priceOk).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				String min=((EditText)shaixuanInput.findViewById(R.id.minPrice)).getText().toString();
				
				String max=((EditText)shaixuanInput.findViewById(R.id.maxPrice)).getText().toString();
				
				if (TextUtils.isEmpty(min)) {
					minCost=null;
				}else{
					minCost=min;
				}
				
				if (TextUtils.isEmpty(max)) {
					maxCost=null;
				}else{
					maxCost=max;
				}
				
				pullDown();
				
				shaixuanInput.setVisibility(View.GONE);
			}
		});
		
		sousuo.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					// 先隐藏键盘
					((InputMethodManager) sousuo.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
							.hideSoftInputFromWindow( getCurrentFocus().getWindowToken(),
									InputMethodManager.HIDE_NOT_ALWAYS);

				 
					pullDown();
					return true;
				}
				return false;
			}
		});
	}

	private OnClickListener sortListenr = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.first:
				order = SORT_COMPREHENSIVE;

				break;

			case R.id.second:
				order = SORT_PRICE_ASC;
				break;
			case R.id.three:
				order = SORT_PRICE_DESC;
				break;
			case R.id.four:
				order = SORT_SALE;
				break;
			}
			showAndHideCheck(order);
			sortSelect.setVisibility(View.GONE);
			pullDown();
		}
	};

	private void showAndHideCheck(String order) {

		if (SORT_COMPREHENSIVE.equals(order)) {
			paixuTitle.setText("综合排序");
			findViewById(R.id.iv1).setVisibility(View.VISIBLE);
		} else {
			findViewById(R.id.iv1).setVisibility(View.INVISIBLE);
		}

		if (SORT_PRICE_ASC.equals(order)) {
			paixuTitle.setText("价格从低到高");
			findViewById(R.id.iv2).setVisibility(View.VISIBLE);
		} else {
			findViewById(R.id.iv2).setVisibility(View.INVISIBLE);
		}

		if (SORT_PRICE_DESC.equals(order)) {
			paixuTitle.setText("价格从高到低");
			findViewById(R.id.iv3).setVisibility(View.VISIBLE);
		} else {
			findViewById(R.id.iv3).setVisibility(View.INVISIBLE);
		}

		if (SORT_SALE.equals(order)) {
			paixuTitle.setText("销量排序");
			findViewById(R.id.iv4).setVisibility(View.VISIBLE);
		} else {
			findViewById(R.id.iv4).setVisibility(View.INVISIBLE);
		}

	}

	protected void pullDown() {
		pageNum = 1;
		MyJsonRequest.Builder<FenGuanGoodsBean> builder = new MyJsonRequest.Builder<FenGuanGoodsBean>().apiVer("100")
				.typeKey("BY_Product_query").param("name", sousuo.getText().toString())

				.param("page_no", "" + pageNum).param("order", order)
				.requestListener(new XRequestListener<FenGuanGoodsBean>() {

					@Override
					public void onResponse(FenGuanGoodsBean arg0) {
						dataList.clear();
						if (arg0.getCount() > 0) {
							dataList.addAll(arg0.getList());

						}
						adapter.setParentId(parentId);
						adapter.notifyDataSetChanged();
						pageNum++;
						xlistview.stopRefresh();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopRefresh();
					}
				});

		if (!TextUtils.isEmpty(minCost)) {
			builder.param("min_cost", minCost);
		}
		if (!TextUtils.isEmpty(maxCost)) {
			builder.param("max_cost", maxCost);
		}
		MyJsonRequest<FenGuanGoodsBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	protected void pullUp() {
		MyJsonRequest.Builder<FenGuanGoodsBean> builder = new MyJsonRequest.Builder<FenGuanGoodsBean>().apiVer("100")
				.typeKey("BY_Product_query").param("sousuo", sousuo.getText().toString())

				.param("page_no", "" + pageNum).param("order", SORT_COMPREHENSIVE)
				.requestListener(new XRequestListener<FenGuanGoodsBean>() {

					@Override
					public void onResponse(FenGuanGoodsBean arg0) {
						if (arg0.getList() != null) {
							dataList.addAll(arg0.getList());

							adapter.notifyDataSetChanged();
							pageNum++;
						}
						xlistview.stopLoadMore();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopLoadMore();
					}
				});

		if (!TextUtils.isEmpty(minCost)) {
			builder.param("min_cost", minCost);
		}
		if (!TextUtils.isEmpty(maxCost)) {
			builder.param("max_cost", maxCost);
		}
		MyJsonRequest<FenGuanGoodsBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
